package com.aiden.cashbook.utils

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.DialogInterface
import android.support.v7.app.AlertDialog
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.TextView
import android.widget.TimePicker
import com.aiden.cashbook.R
import com.aiden.cashbook.extension.toast
import java.util.*
import kotlin.math.max

object AlertDialogUtil {

    object ConfirmDialog {
        private var alertDialog: AlertDialog? = null

        fun show(context: Context, title: String = "", message: String = "",
                 positiveButtonText: String = "确认", onPositiveClickListener: DialogInterface.OnClickListener = DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() },
                 negativeButtonText: String = "取消", onNegativeClickListener: DialogInterface.OnClickListener = DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() }) {
            if (alertDialog == null) {
                val builder = AlertDialog.Builder(context)
                builder.setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(positiveButtonText, onPositiveClickListener)
                        .setNegativeButton(negativeButtonText, onNegativeClickListener)
                alertDialog = builder.create()
            } else {
                alertDialog?.let {
                    it.setTitle(title)
                    it.setMessage(message)
                    it.setButton(DialogInterface.BUTTON_POSITIVE, positiveButtonText, onPositiveClickListener)
                    it.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButtonText, onNegativeClickListener)
                }
            }
            alertDialog?.show()

        }

        fun dismiss() {
            if (alertDialog != null && alertDialog!!.isShowing) {
                alertDialog?.dismiss()
            }
        }
    }

    object EditDialog {

        interface OnConfirmClickListener {
            fun onConfirm(dialog: DialogInterface, input: String)
        }

        private var alertDialog: AlertDialog? = null

        @SuppressLint("SetTextI18n")
        fun show(context: Context, title: String = "提示", text: String = "", hint: String = "", inputType: Int = InputType.TYPE_CLASS_TEXT, maxLength: Int = -1,
                 positiveButtonText: String = "确认", onConfirmClickListener: OnConfirmClickListener) {
            var etInput: EditText? = null
            if (alertDialog == null) {
                val view = LayoutInflater.from(context).inflate(R.layout.layout_edittext, null)
                etInput = view.findViewById(R.id.et_input)
                etInput.inputType = inputType
                etInput.hint = hint
                etInput.setText(text)
                val tvNum = view.findViewById<TextView>(R.id.tv_num)
                if (maxLength > 0) {
                    tvNum.text = "0/$maxLength"
                }
                etInput?.addTextChangedListener(object : TextWatcher {
                    override fun afterTextChanged(s: Editable?) {
                        if (s != null && maxLength > 0) {
                            tvNum.text = "${s.length}/$maxLength"
                            alertDialog?.getButton(DialogInterface.BUTTON_POSITIVE)?.isEnabled = !(s.length > maxLength || s.length == 0)
                        }
                    }

                    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                    }

                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    }

                })
                alertDialog = AlertDialog.Builder(context)
                        .setTitle(title)
                        .setView(view)
                        .setPositiveButton(positiveButtonText) { dialog, _ ->
                            onConfirmClickListener.onConfirm(dialog, etInput.text.toString())
                            dialog.dismiss()
                        }.setNegativeButton(R.string.cancel) { _, _ ->
                            dismiss()
                        }
                        .create()
            } else {
                etInput?.let {
                    it.setText(text)
                    it.hint = hint
                    it.inputType = inputType
                }
                alertDialog?.setTitle(title)
            }

            alertDialog?.show()
        }

        fun dismiss() {
            if (alertDialog != null && alertDialog!!.isShowing) {
                alertDialog?.dismiss()
            }
        }
    }

    fun date(context: Context, onDateSetListener: DatePickerDialog.OnDateSetListener, year: Int = Calendar.getInstance().get(Calendar.YEAR), month: Int = Calendar.getInstance().get(Calendar.MONTH), day: Int = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
        var dateDialog: DatePickerDialog? = null
        if (dateDialog == null) {
            dateDialog = DatePickerDialog(context, onDateSetListener, year, month, day)
        } else {
            dateDialog.updateDate(year, month, day)
        }
        dateDialog.show()
    }

    fun time(context: Context, onTimeSetListener: TimePickerDialog.OnTimeSetListener, hourOfDay: Int = Calendar.getInstance().get(Calendar.HOUR_OF_DAY), minute: Int = Calendar.getInstance().get(Calendar.MINUTE), is24HourView: Boolean = true) {
        val timeDateDialog = TimePickerDialog(context, onTimeSetListener, hourOfDay, minute, is24HourView)
        timeDateDialog.show()
    }

}